package com.wing.ext.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.ext.domain.Site_user;
import com.wing.ext.service.Site_userManager;
import com.wing.sys.domain.Operatelog;
import com.wing.sys.domain.User;
import com.wing.sys.service.OperatelogManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/ext/site_user")
public class Site_userController {

	@Autowired
	private Site_userManager site_userManager;
	@Autowired
	private OperatelogManager operatelogManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/ext/site_user/site_userIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/ext/site_user/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(
			HttpServletRequest request,int currentPage, String pageSize) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  site_userManager.findPageList(currentPage, _iPageSize,siteId);
	}

	/**
	 * 验证login_code是否已被占用
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "vlgname", method = RequestMethod.GET)
	public @ResponseBody String vlgcode(String login_name,String userId) {
		List list = site_userManager.findUsersByLoginName(login_name);
		if(list.size()==0){
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		if(list.size()>1){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Map map = (Map)list.get(0);
		if(!map.get("id").equals(userId)){
			return Constants.DEFAULT_AJAX_FAILURE;
		}else{
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		
	}

	/**
	 * 用户验证
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public @ResponseBody String login(Site_user user,HttpServletRequest request) {
		Map userMap = site_userManager.findSiteUser(user.getSite_id(),user.getLogin_name(),user.getPassword());
		if(userMap==null){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Site_user u = site_userManager.findById((String)userMap.get("id"));
		request.getSession().setAttribute(Constants.SESSION_SITE_USER_ID,u.getId());
		request.getSession().setAttribute(Constants.SESSION_SITE_USER_NAME,u.getLogin_name());
		Operatelog o = new Operatelog();
		o.setDescription(u.getName()+"于"+(new Date())+"登陆 网站用户");
		o.setLog_time(new Date());
		o.setUser_id(u.getId());
		o.setWork_ip(request.getRemoteAddr());
		o.setSuccess(1);
		operatelogManager.saveOperatelog(o);

		return Constants.DEFAULT_AJAX_SUCCESS;
	}

	/**
	 * 用户登出
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	public @ResponseBody String logout(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.SESSION_SITE_USER_ID);
		request.getSession().removeAttribute(Constants.SESSION_SITE_USER_NAME);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id, Model model) {
		Site_user site_user = null;
		if(!id.equals("0")){
			site_user = site_userManager.findById(id);
		}else{
			site_user = new Site_user();
		}
		model.addAttribute("site_user", site_user);
		return "/ext/site_user/site_userForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Site_user site_user,HttpServletRequest request) {
		site_userManager.saveSite_user(site_user);
		request.getSession().setAttribute(Constants.SESSION_SITE_USER_ID,site_user.getId());
		request.getSession().setAttribute(Constants.SESSION_SITE_USER_NAME,site_user.getLogin_name());
		return "success";
	}
	
	@RequestMapping(value = "delsite_users", method = RequestMethod.POST)
	public @ResponseBody String delsite_users(String[] chk) throws IOException {
		site_userManager.deleteSite_users(chk);
		return ""+chk.length;
	}
}

